473,424 Members | 1,817 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,424 software developers and data experts.

Calling Javascript on a Dropdown list

I have a dropdown list as below.

I add an onchnage attribute in my codebehind to call some Javascript.

I want to get the selected text from my dropdown. What am I doing wrong below?

<asp:DropDownList ID="ddlStatus" runat="server" />

In my codebehind

ddlStatus.Attributes["onchange"] = "javascript:SetDateFields();";

<script language="javascript" type="text/javascript">

function SetDateFields()

{

var statusList = document.getElementById("ddlStatus");
var selectedStatus = statusList.options[statusList.selectedIndex].value;
}

</script>

Feb 2 '07 #1
7 33833
C wrote:
I have a dropdown list as below.

I add an onchnage attribute in my codebehind to call some Javascript.

I want to get the selected text from my dropdown. What am I doing wrong below?
Why do you think that you are doing something wrong? What happens when
you try to use the code? Do you get any error message? Have you enabled
Javascript error messages in the browser?
<asp:DropDownList ID="ddlStatus" runat="server" />

In my codebehind

ddlStatus.Attributes["onchange"] = "javascript:SetDateFields();";
The "javascript:" protocol is only used when you put Javascript in an
URL. The only reason that you don't get an error message when you use it
elsewhere, is that it becomes a label instead.

ddlStatus.Attributes["onchange"] = "SetDateFields();";
<script language="javascript" type="text/javascript">

function SetDateFields()

{

var statusList = document.getElementById("ddlStatus");
Have you checked that this is the actual id that the element gets in the
html code?
var selectedStatus = statusList.options[statusList.selectedIndex].value;
You are getting the value instead of the text.
}

</script>

--
Göran Andersson
_____
http://www.guffa.com
Feb 2 '07 #2
Hi,

C wrote:

<snip>
<script language="javascript" type="text/javascript">
Additionally to Göran's remarks, the "language" attribute is deprecated.
You can safely remove it and use only the "type" attribute.

Greetings,
Laurent
--
Laurent Bugnion [MVP ASP.NET]
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
Feb 2 '07 #3
As mentioned on the other forum, aside from the issues adressed by the
others, you are doing anything wrong. Try putting alert(selectedStatus) just
before the closing brace and you will see the value.
Peter

--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"C" wrote:
I have a dropdown list as below.

I add an onchnage attribute in my codebehind to call some Javascript.

I want to get the selected text from my dropdown. What am I doing wrong below?

<asp:DropDownList ID="ddlStatus" runat="server" />

In my codebehind

ddlStatus.Attributes["onchange"] = "javascript:SetDateFields();";

<script language="javascript" type="text/javascript">

function SetDateFields()

{

var statusList = document.getElementById("ddlStatus");
var selectedStatus = statusList.options[statusList.selectedIndex].value;
}

</script>
Feb 2 '07 #4
Hi there,

In addition to Goran's reply, please find working snippet below:

-- begin aspx code --

<asp:DropDownList ID="ddlStatus" runat="server"
AutoPostBack="false" OnChange="SetDateFields()">
<asp:ListItem Text="Status1" Value="Value1"/>
<asp:ListItem Text="Status2" Value="Value2"/>
<asp:ListItem Text="Status3" Value="Value3"/>
</asp:DropDownList>

<script type="text/javascript">
function SetDateFields()
{
var statusList = document.getElementById('<%=ddlStatus.ClientID %>');
var selectedStatus = statusList.options[statusList.selectedIndex].text;

alert('selected status is : ' + selectedStatus);
}
</script>
-- end aspx code --

Note you have to use control.ClientID to get the valid ID (ClientID contains
all the parent controls ids - if i didnt use it and moved dropdown list to a
Panel control it'd stop work). You may be wondering why i put OnChange
attribute to drop down list declaration. This is allowed (even if you get a
warning from schema validation) and it's called 'expando'.

Hope it's clear now
Milosz
"C" wrote:
I have a dropdown list as below.

I add an onchnage attribute in my codebehind to call some Javascript.

I want to get the selected text from my dropdown. What am I doing wrong below?

<asp:DropDownList ID="ddlStatus" runat="server" />

In my codebehind

ddlStatus.Attributes["onchange"] = "javascript:SetDateFields();";

<script language="javascript" type="text/javascript">

function SetDateFields()

{

var statusList = document.getElementById("ddlStatus");
var selectedStatus = statusList.options[statusList.selectedIndex].value;
}

</script>
Feb 2 '07 #5
Hi there,

In addition to Goran's reply, please find working snippet below:

-- begin aspx code --

<asp:DropDownList ID="ddlStatus" runat="server"
AutoPostBack="false" OnChange="SetDateFields()">
<asp:ListItem Text="Status1" Value="Value1"/>
<asp:ListItem Text="Status2" Value="Value2"/>
<asp:ListItem Text="Status3" Value="Value3"/>
</asp:DropDownList>

<script type="text/javascript">
function SetDateFields()
{
var statusList = document.getElementById('<%=ddlStatus.ClientID %>');
var selectedStatus = statusList.options[statusList.selectedIndex].text;

alert('selected status is : ' + selectedStatus);
}
</script>
-- end aspx code --

Note you have to use control.ClientID to get the valid ID (ClientID contains
all the parent controls ids - if i didnt use it and moved dropdown list to a
Panel control it'd stop work). You may be wondering why i put OnChange
attribute to drop down list declaration. This is allowed (even if you get a
warning from schema validation) and it's called 'expando'.

Hope it's clear now
Milosz
"C" wrote:
I have a dropdown list as below.

I add an onchnage attribute in my codebehind to call some Javascript.

I want to get the selected text from my dropdown. What am I doing wrong below?

<asp:DropDownList ID="ddlStatus" runat="server" />

In my codebehind

ddlStatus.Attributes["onchange"] = "javascript:SetDateFields();";

<script language="javascript" type="text/javascript">

function SetDateFields()

{

var statusList = document.getElementById("ddlStatus");
var selectedStatus = statusList.options[statusList.selectedIndex].value;
}

</script>
Feb 2 '07 #6
Hi,

U can acheive that by writing

ddlStatus.Attributes.Add("onchange","javascript:Se tDateFields()")
Bhaskar

"C" <C@discussions.microsoft.comwrote in message
news:59**********************************@microsof t.com...
>I have a dropdown list as below.

I add an onchnage attribute in my codebehind to call some Javascript.

I want to get the selected text from my dropdown. What am I doing wrong
below?

<asp:DropDownList ID="ddlStatus" runat="server" />

In my codebehind

ddlStatus.Attributes["onchange"] = "javascript:SetDateFields();";

<script language="javascript" type="text/javascript">

function SetDateFields()

{

var statusList = document.getElementById("ddlStatus");
var selectedStatus = statusList.options[statusList.selectedIndex].value;
}

</script>

Feb 3 '07 #7
Hi,

Bhaskardeep Khaund wrote:
Hi,

U can acheive that by writing

ddlStatus.Attributes.Add("onchange","javascript:Se tDateFields()")
This is the same as what the OP did

ddlStatus.Attributes["onchange"] = "javascript:SetDateFields();";

Additionally, had you read previous answers, you would have seen that
you should never use "javascript:", except in very very specific cases.

HTH,
Laurent
--
Laurent Bugnion [MVP ASP.NET]
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
Feb 3 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Joseph Barron | last post by:
Here is a SIMPLE problem that I'm trying to solve. It works in Netscape 6.2, but IE6 gives ""No such interface supported." Below are page1.htm and page2.htm . In page1.htm, there are two...
1
by: Rod Early | last post by:
I need to know when the select element's dropdown list is opened (as when the user clicks on the arrow or does ALT-downarrow from the keyboard). Similarly, I need to known when the dropdown list...
6
by: passion_to_be_free | last post by:
This is probably simple, but I can't seem to find it anywhere. I have have some values stored in javascript variables. I have a <select> dropdown list whose options correspond to these values. I...
6
by: Mark | last post by:
I have two dropdown lists. Both have autopostback set to true. In both dropdowns, when you select an item from the list, it redirects to the Value property of the dropdown. Nothing fancy. ...
3
by: RJN | last post by:
Hi The texts in the dropdown are too long and the width is not sufficient to show the entire text. Increasing the width is not an option. Is there a way to show the selected item text as a...
4
by: Dica | last post by:
i've got a dropdown list with various options added to it. i want to add a button that allows the user to "Add New Option' which should create a blank row on the dropdown list and allow the user to...
5
by: jung_h_park | last post by:
From: jung_h_park@yahoo.com Newsgroups: microsoft.public.dotnet.framework.aspnet Subject: Dropdown List not retaining its SelectedValue Date: Mon, 26 Jun 2006 21:02:57 -0700 Hello, My...
11
by: eureka | last post by:
Hi All, I'm training in Servlets, JSP and JavaScript, I have a web page in which there's a "StudentName" textbox and below it is a "Names" Dropdown list. Initially the Textbox is empty and...
1
by: bappadiit | last post by:
I am not sure that what I name it but here is the details of what I want to do: There will be two or more dropdown lists, suppose first one contains faculty name and the 2nd one is course name....
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.